# fake_real_data.py
''' Read the data that the USCG monitor is sending to GAE and write it into
    /var/www/readings.json as if it were being put there by get_readings.py
    '''
# 8/19/10 TS-7500 runs Python 2.5.2 a thus need this:
from __future__ import with_statement

import urllib, time
import simplejson as json
import mac
URL = 'http://roqueislandmonitor.appspot.com/last?mac=00:d0:69:42:a2:0b'
run = True
READINGS = '/var/www/readings.json'
#READINGS = 'readings.json'

while run:
    ok = True
    try:
        f = urllib.urlopen(URL)
    except:
        print 'error opening ',URL
        ok = False
    if ok:
        try:
            s = f.read()
        except:
            print 'error reading ', URL
            f.close()
            ok = False
    if ok:
        f.close()
        try:
            j = json.loads(s)
        except:
            print 'json error: ', URL
            ok = False

    j[u'mac'] = mac.getMacAddress()
    s = json.dumps(j)
    with open(READINGS, 'w') as f:
        f.write(s)
    time.sleep(5)
    # run = False    
